import 'package:OASys/entity/UserVo.dart';
import 'package:flutter/material.dart';

class PersonalInfoEdit extends StatefulWidget {
  final Map arguments;
  PersonalInfoEdit({this.arguments, Key key});

  @override
  _PersonalInfoEditState createState() => _PersonalInfoEditState();
}

class _PersonalInfoEditState extends State<PersonalInfoEdit> {
  void _handleSave() {
    print(123);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(),
        actions: [
          Container(
            padding: EdgeInsets.all(15),
            child: GestureDetector(
              child: Text(
                '保存',
                style: TextStyle(fontSize: 18),
              ),
              onTap: _handleSave,
            ),
          )
        ],
        title: Text("编辑个人信息"),
        centerTitle: true,
        automaticallyImplyLeading: false,
      ),
      body: MyPersonalInfoEdit(
        UserVo.fromJson(widget.arguments),
        save: _handleSave,
      ),
    );
  }
}

class MyPersonalInfoEdit extends StatefulWidget {
  final Function save;

  final UserVo userVo;
  const MyPersonalInfoEdit(this.userVo, {Key key, @required this.save})
      : super(key: key);
  @override
  _MyPersonalInfoEditState createState() => _MyPersonalInfoEditState();
}

class _MyPersonalInfoEditState extends State<MyPersonalInfoEdit> {
  GlobalKey<FormState> formGlobalKey = GlobalKey<FormState>();
  String userName;
  String phone;
  int sex;
  String email;
  UserVo info = new UserVo();
  @override
  void initState() {
    super.initState();
    info = UserVo.fromJson(widget.userVo.toJson());
    print(info.toJson().toString());
    print(widget.userVo.toJson().toString());
  }

  void save() {
    var form = formGlobalKey.currentState;
    if (form.validate()) {
      form.save();
      print(info.toJson().toString());
      print(widget.userVo.toJson().toString());
    }
  }

  void reset() {
    var form = formGlobalKey.currentState;
    form.reset();
    setState(() {
      info = UserVo.fromJson(widget.userVo.toJson());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(50),
      height: 500,
      child: Card(
        shape: RoundedRectangleBorder(
          //形状
          //修改圆角
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        //阴影颜色
        color: Colors.white,
        //阴影高度
        elevation: 4.0,
        child: Container(
          margin: EdgeInsets.all(50),
          child: Form(
            key: formGlobalKey,
            child: Column(
              children: [
                SizedBox(
                  height: 30,
                ),
                TextFormField(
                  initialValue: info.nickname,
                  decoration: InputDecoration(
                    prefixText: "昵称：",
                    prefixStyle: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      letterSpacing: 1,
                    ),
                  ),
                  // ignore: missing_return
                  validator: (content) {
                    if (content.length > 8) {
                      return "昵称过长";
                    } else if (content.length <= 0) {
                      return "昵称过短";
                    }
                  },
                  onSaved: (value) {
                    info.nickname = value;
                  },
                ),
                TextFormField(
                  initialValue: info.phone,
                  decoration: InputDecoration(
                    prefixText: "手机号码：",
                    prefixStyle: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      letterSpacing: 1,
                    ),
                  ),
                  // ignore: missing_return
                  validator: (content) {
                    if (content.length < 13) {
                      return "手机号码格式不正确";
                    }
                  },
                  onSaved: (value) {
                    info.phone = value;
                  },
                ),
                TextFormField(
                  initialValue: info.email,
                  decoration: InputDecoration(
                    prefixText: "邮箱：",
                    prefixStyle: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      letterSpacing: 1,
                    ),
                  ),
                  // ignore: missing_return
                  validator: (content) {
                    if (content.length < 13) {
                      return "邮箱格式不正确";
                    }
                  },
                  onSaved: (value) {
                    info.email = value;
                  },
                ),
                Row(
                  children: [
                    Text(
                      "性别：",
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                        letterSpacing: 1,
                      ),
                    ),
                    Flexible(
                      child: RadioListTile(
                        title: Text('男'),
                        value: 1,
                        groupValue: info.sex,
                        onChanged: (value) {
                          setState(() {
                            info.sex = value;
                          });
                        },
                      ),
                    ),
                    Flexible(
                        child: RadioListTile(
                      title: Text('女'),
                      value: 2,
                      groupValue: info.sex,
                      onChanged: (value) {
                        setState(() {
                          info.sex = value;
                        });
                      },
                    )),
                  ],
                ),
                SizedBox(
                  height: 30,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    RaisedButton(
                      color: Color.fromRGBO(33, 150, 243, 1),
                      onPressed: save,
                      child: Text(
                        "保 存",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                    RaisedButton(
                      color: Color.fromRGBO(103, 194, 58, 1),
                      onPressed: reset,
                      child: Text(
                        "重 置",
                        style: TextStyle(color: Colors.white),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
